jQuery(function($){
  setDateTimePicker()

  $("a.js-to-confirm").on("click", function(){
    var id = $(this).data("id");
    var _this = this
    var csrf = $("input#js-ruiqu-store-orders-index").val();
    $.ajax({
      url: "/api/store_orders/"+ id +"/confirm",
      method: "put",
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $("span.js-confirm-status-"+ id).html("处理状态:&nbsp已确认");
          $(_this).replaceWith("<span>订单确认&nbsp&nbsp</span>")
        }
      }
    });
  })

  $("a.js-to-process").on("click", function(){
    var id = $(this).data("id");
    var _this = this
    var csrf = $("input#js-ruiqu-store-orders-index").val();
    $.ajax({
      url: "/api/store_orders/"+ id +"/to_process",
      method: "put",
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $(_this).siblings("a.js-to-mail").data("status", "processing");
          $(_this).nextAll().last().replaceWith("<span>取消</span>")
          $(_this).prev().replaceWith("<span>订单确认&nbsp&nbsp</span>")
          $(_this).replaceWith("<span>处理订单</span>")
          $("span.js-confirm-status-"+ id).html("处理状态:&nbsp处理中");
        }
      }
    });
  })

  $("a.js-to-mail").on("click", function(){
    console.log(this);
    var id = $(this).data("id");
    var status = $(this).data("status");
    var _this = this
    var csrf = $("input#js-ruiqu-store-orders-index").val();
    console.log(status);
    if (status == "processing"){
      $.ajax({
        url: "/api/store_orders/"+ id +"/to_mail",
        method: "put",
        headers: {
          'X-CSRF-Token': csrf
        },
        success: function(data){
          if(data.status){
            $(_this).replaceWith("<span>确认发货</span>")
            $("span.js-confirm-status-"+ id).html("处理状态:&nbsp已发货");
          }
        }
      });
    }else {
      BootstrapDialog.alert("请确定处理订单!");
    }
  })


  $("a.js-to-cancel").on("click", function(){
    var id = $(this).data("id");
    var _this = this
    var csrf = $("input#js-ruiqu-store-orders-index").val();
    $.ajax({
      url: "/api/store_orders/"+ id +"/cancel",
      method: "put",
      headers: {
        'X-CSRF-Token': csrf
      },
      success: function(data){
        if(data.status){
          $(_this).siblings("a.js-to-confirm").replaceWith("<span>订单确认&nbsp&nbsp</span>")
          $(_this).siblings("a.js-to-process").replaceWith("<span>处理订单</span>")
          $(_this).siblings("a.js-to-mail").replaceWith("<span>确认发货&nbsp&nbsp</span>")
          $(_this).replaceWith("<span>取消</span>")
          $("span.js-confirm-status-"+ id).html("处理状态:&nbsp已取消");
        }
      }
    });
  })
})
